package top.happylaishop.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import top.happylaishop.shop.common.Const;
import top.happylaishop.shop.dao.CategoryDao;
import top.happylaishop.shop.dao.ProductDao;
import top.happylaishop.shop.dao.ProductDetailDao;
import top.happylaishop.shop.dto.ProductDetailDto;
import top.happylaishop.shop.dto.ProductDto;
import top.happylaishop.shop.dto.ProductListDto;
import top.happylaishop.shop.dto.UserDto;
import top.happylaishop.shop.pojo.Category;
import top.happylaishop.shop.pojo.Product;
import top.happylaishop.shop.pojo.ProductDetail;
import top.happylaishop.shop.service.ProductService;
import top.happylaishop.shop.utils.PageDto;
import top.happylaishop.shop.utils.resp.BaseResponse;
import top.happylaishop.shop.utils.resp.RestResponse;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Value("${web.file.prefix}")
    private String webFilePrefix;

    @Autowired
    private ProductDao productDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private ProductDetailDao productDetailDao;

    @Override
    public void listPage(PageDto<ProductListDto> productListDtoPageDto, Integer categoryId, String keyword) {
        Page<Product> page = new Page<>(productListDtoPageDto.getPage(), productListDtoPageDto.getSize());
        Page<Product> productPage = productDao.selectPage(page, null);
        productListDtoPageDto.setTotal(productPage.getTotal());
        List<ProductListDto> productListDtoList = new ArrayList<>();
        for (Product product : productPage.getRecords()) {
            productListDtoList.add(assembleProductListVo(product));
        }
        productListDtoPageDto.setList(productListDtoList);
    }

    private ProductListDto assembleProductListVo(Product product) {
        ProductListDto productListDto = new ProductListDto();
        productListDto.setId(product.getId());
        productListDto.setName(product.getName());
        productListDto.setCategoryId(product.getCategoryId());
        productListDto.setImageHost(webFilePrefix);
        productListDto.setMainImage(product.getMainImage());
        productListDto.setPrice(product.getPrice());
        productListDto.setSubtitle(product.getSubtitle());
        productListDto.setStatus(product.getStatus());
        return productListDto;
    }

    @Override
    public BaseResponse saveOrUpdateProduct(ProductDto productDto, UserDto currentUser) {
        if(StrUtil.isNotBlank(productDto.getSubImages())){
            String[] split = productDto.getSubImages().split(",");
            if(split.length > 0){
                productDto.setMainImage(split[0]);
            }
        }
        Product productDb = BeanUtil.copyProperties(productDto, Product.class);
        productDb.setUpdateTime(new Date());
        productDb.setUpdateUserId(currentUser.getId());
        if(productDto.getId() != null){
            productDao.updateById(productDb);
        } else {
            productDb.setCreateTime(productDb.getUpdateTime());
            productDb.setCreateUserId(currentUser.getId());
            productDao.insert(productDb);
            if(StrUtil.isNotBlank(productDto.getDetail())){
                ProductDetail productDetailDb = new ProductDetail();
                productDetailDb.setProductId(productDb.getId());
                productDetailDb.setProductDetail(productDto.getDetail());
                productDetailDao.insert(productDetailDb);
            }
        }
        return BaseResponse.success();
    }

    @Override
    public BaseResponse updateSaleStatusById(ProductDto productDto, UserDto currentUser) {
        Product product = new Product();
        product.setUpdateTime(new Date());
        product.setUpdateUserId(currentUser.getId());
        product.setStatus(productDto.getStatus());
        product.setId(productDto.getId());
        productDao.updateById(product);
        return BaseResponse.success();
    }

    @Override
    public RestResponse<ProductDetailDto> getDetailById(Integer id) {
        Product product = productDao.selectById(id);
        if(product == null){
            return RestResponse.failed(Const.ERROR_MSG_NOTFOUND);
        }
        return RestResponse.defaultSucessData(assembleProductDetailVo(product));
    }

    private ProductDetailDto assembleProductDetailVo(Product product){
        ProductDetailDto productDetailDto = new ProductDetailDto();
        productDetailDto.setId(product.getId());
        productDetailDto.setSubtitle(product.getSubtitle());
        productDetailDto.setPrice(product.getPrice());
        productDetailDto.setMainImage(product.getMainImage());
        productDetailDto.setSubImages(product.getSubImages());
        productDetailDto.setCategoryId(product.getCategoryId());
        ProductDetail productDetail = productDetailDao.selectById(product.getId());
        if(productDetail != null){
            productDetailDto.setDetail(productDetail.getProductDetail());
        }
        productDetailDto.setName(product.getName());
        productDetailDto.setStatus(product.getStatus());
        productDetailDto.setStock(product.getStock());

        productDetailDto.setImageHost(webFilePrefix);

        Category category = categoryDao.selectById(productDetailDto.getCategoryId());
        if(category == null){
            productDetailDto.setParentCategoryId(0);
        } else {
            productDetailDto.setParentCategoryId(category.getParentId());
        }
        productDetailDto.setCreateTime(DateUtil.formatDateTime(product.getCreateTime()));
        productDetailDto.setUpdateTime(DateUtil.formatDateTime(product.getUpdateTime()));
        return productDetailDto;
    }
}
